package com.jiuyun.auth.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 阿里连接池
 * 
 * @author XiaoFan
 *
 */
public class AliPool {

	static DruidDataSource ds;
	// 只会运行一次
	static {
		String fname = "druid.properties";
		InputStream file = AliPool.class.getClassLoader().getResourceAsStream(fname);
		Properties prop = new Properties();
		try {
			prop.load(file);
		} catch (IOException e) {

			e.printStackTrace();
		}
		// 读取文件
		String driver = prop.getProperty("driverClassName");
		String url = prop.getProperty("url");
		String name = prop.getProperty("username");
		String pass = prop.getProperty("password");
		String init = prop.getProperty("initialSize");
		String minIdle = prop.getProperty("minIdle");
		String maxActive = prop.getProperty("maxActive");
		String maxWait = prop.getProperty("maxWait");

//		 System.out.println(url);
		// 配置连接池
		ds = new DruidDataSource();
		ds.setDriverClassName(driver);
		ds.setUrl(url);
		ds.setUsername(name);
		ds.setPassword(pass);
		ds.setInitialSize(Integer.parseInt(init));
		ds.setMinIdle(Integer.parseInt(minIdle));
		ds.setMaxActive(Integer.parseInt(maxActive));
		ds.setMaxWait(Long.parseLong(maxWait));

//		 Connection conn =  ds.getConnection();
//		 System.out.println(conn);
//		 conn.close();

	}

	// 池里取连接
	public static Connection getConn() {
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

}
